package com.example.gaoyang.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class TheardUtil {

    /**
     * 等待线程结束
     *
     * @param futureMap
     * @return
     */
    public static  void waitThead(Map<String, Future> futureMap,int i) {

        while (futureMap.size() > i) {
            Iterator<Map.Entry<String, Future>> iterator = futureMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Future> next = iterator.next();
                Future value = next.getValue();
                if (value.isDone()||value.isCancelled()) iterator.remove();
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public static List<Integer>futursGet(Map<Integer,Future<Boolean>> map){
        List<Integer> integerList=new ArrayList<>();
        for (Map.Entry<Integer, Future<Boolean>> entry : map.entrySet()) {
            try {
                //获取上传结果
                if (!entry.getValue().get()){
                    integerList.add(entry.getKey());
                }
            } catch (InterruptedException e) {

            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
        return integerList;
    }

}
